#include "TreasuryFuturesChartControl.h"
#include "TreasuryFuturesChartElement.h"
#include "../data/TreasuryFuturesData.h"
#include "../StringResource.h"
#include <QHBoxLayout>
#include <qevent.h>
#include <widgets/STips.h>
#include <core/StyleMgr.h>

CFixedNatFutureChartControl::CFixedNatFutureChartControl(QWidget* p, CFixedNatFutureData* data, bool isYestoday)
	: QWidget(p), 
	  m_data(data),
	  m_isYesterday(isYestoday)
{
	setMinimumHeight(117);

	m_mousePos = -1;
	m_isShowCross = true;

	for (int nL=0; nL<ele_Max; nL++){
		if (nL == ele_Border) m_chartElement[nL] = new CFixedFutureChartBorder();
		else if (nL == ele_Vol) m_chartElement[nL] = new CFixedFutureChartBar();
		else if (nL == ele_Point) m_chartElement[nL] = new CFixedFutureChartPrcLine();
		else if (nL == ele_OpenInter) m_chartElement[nL] = new CFixedFutureChartOpenInst();
		else if (nL == ele_XPosition) m_chartElement[nL] = new CFixedFutureChartTime();
		if (m_chartElement[nL]) m_chartElement[nL]->setData(m_data, !m_isYesterday);
	}

	setMouseTracking(true);
}

CFixedNatFutureChartControl::~CFixedNatFutureChartControl()
{
	for (int nL=0; nL<ele_Max; nL++){
		if (m_chartElement[nL]){
			delete m_chartElement[nL];
		}
	}
}

void CFixedNatFutureChartControl::paintEvent(QPaintEvent* event)
{
	QStylePainter painter(this);
	QRect rcClient = rect();
	//painter.fillRect(rcClient, QColor(255,0,0));

	rcClient.setTop(rcClient.top() + 1);
	int nXPosHgt = 15;
	QRect rcXpos = rcClient;
	if (1){//如果需要X坐标，默认需要
		rcClient.setBottom(rcXpos.bottom() - nXPosHgt);
		rcXpos.setTop(rcXpos.bottom() - nXPosHgt);
		rcXpos.adjust(0, 2, 0, 0);
	}

	QRect rcWorkArea =  rcClient;
	rcWorkArea.setTop(m_rcMark.bottom());
	QRect rcPoint = rcWorkArea;
	QRect rcVolum = rcWorkArea;
	rcPoint.setBottom(rcClient.top() + rcWorkArea.height() * 0.6667);
	rcVolum.setTop(rcClient.top() + rcWorkArea.height() * 0.6667);

	QRect rcDraw;
	for (int nL=0; nL<ele_Max; nL++){
		if (nL == ele_Border) rcDraw = rcClient;
		else rcDraw = rcWorkArea;

		if (nL == ele_Point) rcDraw = rcPoint;
		else if (nL == ele_Vol || nL == ele_OpenInter) rcDraw = rcVolum;
		else if (nL == ele_XPosition) rcDraw = rcXpos;

		if (m_chartElement[nL]){
			m_chartElement[nL]->drawElement(&painter, rcDraw, m_curKey, this);
		}
	}	

	QPen pen(QBrush(QColor(19, 108, 94)), 1, Qt::DotLine);
	painter.setPen(pen);
	painter.drawLine(QPoint(m_mousePos, rcClient.top()), QPoint(m_mousePos, rcClient.bottom()));
}

void CFixedNatFutureChartControl::resizeEvent(QResizeEvent* event)
{
	QWidget::resizeEvent(event);
	m_rcMark = rect();
	m_rcMark.setBottom(m_rcMark.top() + 20);
}

void CFixedNatFutureChartControl::onRefresh()
{
	update();
}

void CFixedNatFutureChartControl::mouseMoveEvent(QMouseEvent* event)
{
	if (m_isShowCross){
		m_mousePos = event->x();
		m_curKey = getCurrentPointKey(rect());
		update();
		showTips(event->screenPos().toPoint(), m_curKey);
	}
}

void CFixedNatFutureChartControl::showTips(const QPoint& pt, int curKey)
{
	if (curKey<0 || !m_isShowCross) return;

	QString tips = NewStringTable::strFixedNatChart(0);
	if (!m_data->GetCurrentTFID().isEmpty()) {
		tips += " " + m_data->GetCurrentTFID();
	}
	CFixedNatFutureTFData tfData(false);
	if (!m_data || !m_data->GetCurrentData(tfData)) return;
	std::map<int, CFixedNatFuturePoint>& mapPoint = (m_isYesterday ? tfData.m_mapPointYes : tfData.m_mapPoint);
	std::map<int, CFixedNatFuturePoint>::const_iterator itr = mapPoint.find(curKey);
	if (itr == mapPoint.end()) return;
	if (itr->second.m_fPrice < 1) return;

	int year = 0;
	int month = 0;
	int day = 0;
	int hour = itr->first / 100;
	int minute = itr->first % 100;
	if (m_isYesterday) {
		if (tfData.m_nYestodayDate > 0) {
			year = tfData.m_nYestodayDate / 10000;
			month = (tfData.m_nYestodayDate / 100) % 100;
			day = tfData.m_nYestodayDate % 100;
		}
	}
	else {
		if (tfData.m_nTodayDate > 0) {
			year = tfData.m_nTodayDate / 10000;
			month = (tfData.m_nTodayDate / 100) % 100;
			day = tfData.m_nTodayDate % 100;
		}
	}
	QString strDate = QString::asprintf("%02d-%02d  %02d:%02d", month, day, hour, minute);
	tips += "\n";
	tips += NewStringTable::strFixedNatChart(1);
	tips += " " + strDate;
	if (itr->second.m_fPrice > 0.001) {
		QString strPrc = QString::asprintf("%.3f", itr->second.m_fPrice);
		tips += "\n";
		tips += NewStringTable::strFixedNatChart(2);
		tips += " " + strPrc;
	}
	if (itr->second.m_nVolum > 0) {
		QString strCount = QString::asprintf("%d", itr->second.m_nVolum);
		tips += "\n";
		tips += NewStringTable::strFixedNatChart(3);
		tips += " " + strCount;
	}
	if (itr->second.m_dbOpenInt > 0.9) {
		QString strOpenIn = QString::asprintf("%.0f", itr->second.m_dbOpenInt);
		tips += "\n";
		tips += NewStringTable::strFixedNatChart(4);
		tips += " " + strOpenIn;
	}

	std::vector<qb::QBTipCustomItem> vctCustomItem;
	qb::QBTipCustomItem item;
	item.clrNormal = QColor(223, 174, 76, 255);
	item.col = 1;
	for (int i=0; i<5; i++){
		item.row = i;
		vctCustomItem.push_back(item);
		if (i == 1) {
			item.col = 2;
			vctCustomItem.push_back(item);
		}
	}
	qb::QBTips::showText(pt, tips, this, &vctCustomItem);
}

void CFixedNatFutureChartControl::leaveEvent(QEvent* event)
{
	m_mousePos = -1;
	m_curKey = -1;
	update();
}

int CFixedNatFutureChartControl::getCurrentPointKey(const QRect& rect)
{
	int key = -1;
	if (!m_isShowCross || m_mousePos < 0 || !m_data) return key;
	CFixedNatFutureTFData tfData(false);
	if (m_data && m_data->GetCurrentData(tfData)){
		std::map<int, CFixedNatFuturePoint>& mapPoint = (m_isYesterday ? tfData.m_mapPointYes : tfData.m_mapPoint);
		int nSz = mapPoint.size();
		double dbGap = rect.width()/(double)nSz;
		QRect rcPoint = rect;
		int nLoop = 0;
		std::map<int, CFixedNatFuturePoint>::const_iterator itr = mapPoint.begin();
		for (; itr != mapPoint.end(); itr++, nLoop++){
			if (m_mousePos>=rect.left()+nLoop*dbGap && m_mousePos<=rect.left()+(nLoop+1)*dbGap) {
				return itr->first;
			}
		}
	}
	return key;
}
